package com.wenli.spinnerdemo;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnItemClickListener {

    private List<String> numberList;
    private EditText etNumber;
    private NumberAdapter mAdapter;
    private PopupWindow pw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etNumber = (EditText) findViewById(R.id.et_number);
    }

    /**
     * 弹出选择号码的对话框
     *
     * @param v
     */
    public void showNumberList(View v) {
        // 初始化ListView控件和里边的数据
        ListView mListView = initListView();

        // 弹出一个PopupWindow的窗体, 把ListView作为其内容部分显示.
        pw = new PopupWindow(mListView, etNumber.getWidth() - 4, 300);

        // 设置可以使用焦点
        pw.setFocusable(true);

        // 设置popupwindow点击外部可以被关闭
        pw.setOutsideTouchable(true);
        // 设置一个popupWindow的背景
        pw.setBackgroundDrawable(new BitmapDrawable());

        // 把popupwindow显示出来, 显示的位置是: 在输入框的下面, 和输入框是连着的.
        pw.showAsDropDown(etNumber, 2, -5);
    }

    /**
     * 初始化一个Listview
     *
     * @return
     */
    private ListView initListView() {
        ListView mListView = new ListView(this);
        mListView.setDividerHeight(0);
        mListView.setBackgroundResource(R.drawable.listview_background);
        // 去掉右侧垂直滑动条
        mListView.setVerticalScrollBarEnabled(false);
        mListView.setOnItemClickListener(this);

        // 模拟数据
        numberList = new ArrayList<String>();
        for (int i = 0; i < 30; i++) {
            numberList.add("100000" + i);
        }

        // 设置适配器展示数据
        mAdapter = new NumberAdapter();
        mListView.setAdapter(mAdapter);
        return mListView;
    }

    class NumberAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return numberList.size();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            NumberHolder mHolder = null;
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.listview_item, null);
                mHolder = new NumberHolder(); // 缓存类
                mHolder.tvNumber = (TextView) convertView.findViewById(R.id.tv_listview_item_number);
                mHolder.ibDelete = (ImageButton) convertView.findViewById(R.id.ib_listview_item_delete);
                // 把Holder类设置给convertView对象.
                convertView.setTag(mHolder);
            } else {
                mHolder = (NumberHolder) convertView.getTag();
            }

            mHolder.tvNumber.setText(numberList.get(position));
            mHolder.ibDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    System.out.println("删除: " + position);

                    numberList.remove(position);
                    mAdapter.notifyDataSetChanged();

                    // 如果全部的号码都被删除了, popupwindow应该被关闭掉.
                    if (numberList.size() == 0) {
                        pw.dismiss();
                    }
                }
            });
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    class NumberHolder {
        public TextView tvNumber;
        public ImageButton ibDelete;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        System.out.println("onItemClick: " + position);

        String number = numberList.get(position);
        etNumber.setText(number);
        pw.dismiss();
    }
}
